package com.sie.demo.controller;

import com.sie.demo.entity.RetResponse;
import com.sie.demo.entity.RetResult;
import com.sie.demo.entity.Role;
import com.sie.demo.entity.vo.PageRoleVo;
import com.sie.demo.entity.vo.PageUserVo;
import com.sie.demo.entity.vo.QueryRoleVo;
import com.sie.demo.service.RoleMenuService;
import com.sie.demo.service.RoleService;
import com.sie.demo.service.impl.RoleServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author liaozhifeng
 * @date 2021/8/4 14:26
 * @Version 1.0
 */
@Controller
@RestController
@RequestMapping("role")
@Slf4j
public class RoleController {

    @Autowired
    RoleServiceImpl roleService;
    @Autowired
    RoleMenuService roleMenuService;

    @GetMapping("selectAll")
    public RetResult<PageRoleVo> selectAll(long currentPage, long pageSize, QueryRoleVo queryRoleVo) {
        PageRoleVo pageUserVo  = roleService.selectAll(currentPage,pageSize,queryRoleVo);
        return RetResponse.makeOKRsp(pageUserVo);
    }
    @GetMapping("getOne")
    public  RetResult<Role> getOne(String roleName) {
        Role role = roleService.getOne(roleName);
            return RetResponse.makeOKRsp(role);
    }
    @PostMapping("add")
    RetResult<Role> addRole(Role role) {
//        获取当前系统时间
        Date date = new Date();
//        添加到Oracle数据库中的时间格式为：【 年/月/日  时:分:秒 】
        java.sql.Timestamp  createTime =new java.sql.Timestamp (date.getTime());
        role.setCreateTime(createTime);
        role.setUpdateTime(createTime);
        int i = roleService.addRole(role);
        if (i == 1) {
            return RetResponse.makeOKRsp("插入成功",role,"");
        } else {
            return RetResponse.makeRsp(500, "服务器异常");
        }
    }
    @PostMapping("edit")
    RetResult<Role> editRole(Role role){
        //        获取当前系统时间
        Date date = new Date();
//        添加到Oracle数据库中的时间格式为：【 年/月/日  时:分:秒 】
        java.sql.Timestamp  updateTime =new java.sql.Timestamp (date.getTime());
        role.setUpdateTime(updateTime);

        int i = roleService.editRole(role);
        if (i == 1) {
            return RetResponse.makeOKRsp("修改成功",role,"");
        } else {
            return RetResponse.makeRsp(500, "服务器异常");
        }
    }

    @DeleteMapping("delete")
    RetResult<Role> deleteRoles(String[] ids){
        Integer isDelete = roleService.deleteRoleByIds(ids);
        if (isDelete != 0){
            return RetResponse.makeRsp(200,"删除成功");
        }else {
            return RetResponse.makeRsp(500,"删除失败");
        }
    }
    //    给角色设置权限
    @PutMapping("setRolePower")
    public RetResult<List> setRolePower(Integer roleId, String[] menuIds) {
        if (menuIds.length!=0){
            if (roleId!=null){
                roleMenuService.deleteByRoleId(roleId);
            }
            roleMenuService.addMenuByRoleId(roleId,menuIds);
            return RetResponse.makeRsp(200, "设置成功");
        }else {
            return RetResponse.makeRsp(200, "请选择至少一项权限");
        }

    }

}
